package com.xzzz.irda.codegen.utils;

import java.io.*;
import java.nio.charset.Charset;

/**
 * IO 工具类
 *
 * @author xzzz
 * @version 0.0.1
 */
public class IoUtil {
    private static final int DEFAULT_BUFFER_SIZE = 8192;

    /**
     * 数据写出
     *
     * @param data         数据
     * @param outputStream 字节输出流
     * @throws IOException 流异常
     */
    public static void write(String data, OutputStream outputStream) throws IOException {
        write(data, outputStream, Charset.defaultCharset());
    }

    /**
     * 数据写出
     *
     * @param data         数据
     * @param outputStream 字节输出流
     * @param charsetName  字符集名称
     * @throws IOException 流异常
     */
    public static void write(String data, OutputStream outputStream, String charsetName) throws IOException {
        write(data, outputStream, Charset.forName(charsetName));
    }


    /**
     * 数据写出
     *
     * @param data         数据
     * @param outputStream 字节输出流
     * @param charset      字符集
     * @throws IOException 流异常
     */
    public static void write(String data, OutputStream outputStream, Charset charset) throws IOException {
        if (data != null) {
            outputStream.write(data.getBytes(charset.name()));
        }

    }

    /**
     * 数据写出
     *
     * @param inputStream  字节输入流
     * @param outputStream 字节输出流
     * @throws IOException 流异常
     */
    public static void write(InputStream inputStream, OutputStream outputStream) throws IOException {
        write(inputStream, outputStream, -1);
    }

    /**
     * 数据写出
     *
     * @param inputStream  字节输入流
     * @param outputStream 字节输出流
     * @param bufferSize   缓存大小
     * @throws IOException 流异常
     */
    public static void write(InputStream inputStream, OutputStream outputStream, int bufferSize) throws IOException {
        if (bufferSize <= 0) {
            bufferSize = DEFAULT_BUFFER_SIZE;
        }

        byte[] buffer = new byte[bufferSize];

        int amount;
        while ((amount = inputStream.read(buffer)) >= 0) {
            outputStream.write(buffer, 0, amount);
        }
    }

    /**
     * 数据写出
     *
     * @param data   数据
     * @param writer 字符输出流
     * @throws IOException 流异常
     */
    public static void write(String data, Writer writer) throws IOException {
        if (data != null) {
            writer.write(data);
        }
    }

    /**
     * 数据写出
     *
     * @param reader 字符输入流
     * @param writer 字符输出流
     * @throws IOException 流异常
     */
    public static void write(Reader reader, Writer writer) throws IOException {
        write((Reader) reader, (Writer) writer, -1);
    }

    /**
     * 数据写出
     *
     * @param reader     字符输入流
     * @param writer     字符输出流
     * @param bufferSize 缓存大小
     * @throws IOException 流异常
     */
    public static void write(Reader reader, Writer writer, int bufferSize) throws IOException {
        if (bufferSize <= 0) {
            bufferSize = DEFAULT_BUFFER_SIZE;
        }

        char[] buffer = new char[bufferSize];

        int amount;
        while ((amount = reader.read(buffer)) >= 0) {
            writer.write(buffer, 0, amount);
        }
    }

    /**
     * 读入数据
     *
     * @param inputStream 字节输入流
     * @return String 读取到的字符串数据
     * @throws IOException IO 异常
     */
    public static String read(InputStream inputStream) throws IOException {
        return read(inputStream, null, -1);
    }

    /**
     * 读入数据
     *
     * @param inputStream 字节输入流
     * @param encoding    编码
     * @return String 读取到的字符串数据
     * @throws IOException IO 异常
     */
    public static String read(InputStream inputStream, String encoding) throws IOException {
        return read(inputStream, encoding, -1);
    }

    /**
     * 读入数据
     *
     * @param inputStream 字节输入流
     * @param encoding    编码
     * @param bufferSize  缓存大小
     * @return String 读取到的字符串数据
     * @throws IOException IO 异常
     */
    public static String read(InputStream inputStream, String encoding, int bufferSize) throws IOException {
        Reader reader = encoding == null ? new InputStreamReader(inputStream) : new InputStreamReader(inputStream, encoding);
        return read(reader, bufferSize);
    }

    /**
     * 读入数据
     *
     * @param reader 字符输入流
     * @return String 读取到的字符串数据
     * @throws IOException IO 异常
     */
    public static String read(Reader reader) throws IOException {
        return read(reader, -1);
    }

    /**
     * 读入数据
     *
     * @param reader     字符输入流
     * @param bufferSize 缓存大小
     * @return String 读取到的字符串数据
     * @throws IOException IO 异常
     */
    public static String read(Reader reader, int bufferSize) throws IOException {
        if (bufferSize <= 0) {
            bufferSize = DEFAULT_BUFFER_SIZE;
        }
        StringWriter writer = new StringWriter();
        write((Reader) reader, (Writer) writer, bufferSize);
        return writer.toString();
    }

    /**
     * 流关闭
     *
     * @param closeable 可关闭的流
     * @throws IOException 流异常
     */
    public static void close(Closeable closeable) throws IOException {
        if (closeable != null) {
            closeable.close();
        }
    }
}
